package com.hexinfo.dmpro.monitor.service;

import com.hexinfo.dmpro.common.base.BaseService;
import com.hexinfo.dmpro.common.base.Page;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.monitor.dao.CmYarnPoolDao;
import com.hexinfo.dmpro.monitor.model.cm.CountResourceInfo;
import com.hexinfo.dmpro.monitor.model.cm.DetailResourceInfo;
import com.hexinfo.dmpro.monitor.model.cm.ResourceBaseReqModel;
import com.hexinfo.dmpro.monitor.model.cm.YarnResourceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author peng
 * @date 2022/2/28 14:32
 * 获取IMPALA 信息
 */
@Service
public class CmYarnService extends BaseService {

    @Autowired
    private CmYarnPoolDao cmYarnPoolDao;

    /**
     * 查询yarn资源使用top信息
     */
    public List<YarnResourceInfo> selYarnPoolTop(ResourceBaseReqModel req){
        //默认查新表，有时间分区就查总表
        List<YarnResourceInfo> selYarnPoolTopList = null;
        Page page = new Page();
        page.setPageIndex(0);
        page.setSize(10);
        page.setSort("id,desc");
        ResourceBaseReqModel reqModel = new ResourceBaseReqModel();
        reqModel.setCmFlag(req.getCmFlag());
        reqModel.setClusterFlag(req.getClusterFlag());
        if(0==req.getTopNum()){
            PageResult pageResult = selYarnPoolDetail(page, reqModel);
            req.setTopNum((int) pageResult.getTotalElements());
        }
        if ("".equals(req.getStartTime()) || "".equals(req.getEndTime())){
            selYarnPoolTopList = cmYarnPoolDao.selYarnPoolTop(req);
        }else {
            selYarnPoolTopList = cmYarnPoolDao.selYarnPoolTopRange(req);
        }
        return selYarnPoolTopList;
    }

    /**
     * 查询yarn资源使用top信息
     * @param req
     * @return
     */
    public List<YarnResourceInfo> selYarnCpuTop(ResourceBaseReqModel req){
        //默认查新表，有时间分区就查总表
        List<YarnResourceInfo> selYarnCpuTopList = null;
        Page page = new Page();
        page.setPageIndex(0);
        page.setSize(10);
        page.setSort("id,desc");
        ResourceBaseReqModel reqModel = new ResourceBaseReqModel();
        reqModel.setCmFlag(req.getCmFlag());
        reqModel.setClusterFlag(req.getClusterFlag());
        if(0==req.getTopNum()){
            PageResult pageResult = selYarnPoolDetail(page, reqModel);
            req.setTopNum((int) pageResult.getTotalElements());
        }
        if ("".equals(req.getStartTime()) || "".equals(req.getEndTime())){
            selYarnCpuTopList = cmYarnPoolDao.selYarnCpuTop(req);
        }else {
            selYarnCpuTopList = cmYarnPoolDao.selYarnCpuTopRange(req);
        }
        return selYarnCpuTopList;
    }

    /**
     * 查询yarn资源池统计信息
     */
    public CountResourceInfo selYarnPoolAllCount(ResourceBaseReqModel req){
        return cmYarnPoolDao.selYarnPoolAllCount(req);
    }

    /**
     * 查询yarn资源池统计信息
     */
    public PageResult selYarnPoolAll(Page page,ResourceBaseReqModel req){
        return selectPage(cmYarnPoolDao, "selYarnPoolYcCount", page, req);
    }
    /**
     * 查询yarn资源池异常统计信息
     */
    public List<CountResourceInfo> selYarnPoolYcCount(ResourceBaseReqModel req){
        return cmYarnPoolDao.selYarnPoolYcCount(req);
    }

    /**
     * 查询yarn资源池异常信息
     */
    public PageResult selYarnPoolYc(Page page,ResourceBaseReqModel req){
        return selectPage(cmYarnPoolDao, "selYarnPoolYc", page, req);
    }

    /**
     * 查询yarn资源使用信息
     */
    public PageResult selYarnPoolDetail(Page page, ResourceBaseReqModel req) {
        return selectPage(cmYarnPoolDao, "selYarnPoolDetail", page, req);
    }

    /**
     * 查询yarn 单个资源使用信息
     */
    public List<YarnResourceInfo> selYarnPoolSyl(ResourceBaseReqModel req){
        return cmYarnPoolDao.selYarnPoolSyl(req);
    }

    /**
     * 获取集群资源池信息
     */
    public List<DetailResourceInfo> queryRootPool(String cmFlag,String clusterFlag){
        return cmYarnPoolDao.queryRootPool(cmFlag,clusterFlag);
    }

}
